package com.example.socket.longlink;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hej
 * @date 2024/9/11 18:47
 */
public class LongConnectionClient2 {
    public static void main(String[] args) {
        //String serverAddress = "10.67.70.169";
        String serverAddress = "127.0.0.1";
        int port = 12345;

        try {
            Socket socket = new Socket(serverAddress, port);
            System.out.println("客户端2已连接到服务器：" + serverAddress + " 端口：" + port);



            DataInputStream in = new DataInputStream(socket.getInputStream());

            //FileWriter fileWriter = new FileWriter("wowowo.xml");
            while (true) {
                String xmlLine = "";
                byte[] buf = new byte[1024];
                int tag = in.read(buf);
                if (tag != -1) {
                    System.out.println(new String(buf, 0, tag));
                    xmlLine += new String(buf, 0, tag);
                }
                //fileWriter.write(xmlLine);
                System.out.println("XML文件保存成功！");
            }
            //in.close();






        } catch (IOException e) {
            e.printStackTrace();
        }   catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void getXmlNode(Map<String,String> keyValue, NodeList nodeList, String sourceStr){
        for (int i = 0; i < nodeList.getLength(); i++){
            Node item = nodeList.item(i);
            String nodeName = item.getNodeName();
            if ( item.getNodeType() != Node.ELEMENT_NODE){
                continue;
            }
            if (StringUtils.isNotEmpty(keyValue.get(nodeName))){
                continue;
            }
            NodeList childNodes = item.getChildNodes();
            if (ObjectUtils.isNotEmpty(childNodes)){
                String fromStr = "<"+nodeName+">";
                String toStr = "</"+nodeName+">";
                String value = sourceStr.substring(sourceStr.indexOf(fromStr)+fromStr.length(),
                        sourceStr.lastIndexOf(toStr));
                getXmlNode(keyValue,childNodes,sourceStr);
                keyValue.put(nodeName,value);
            }
        }
    }

    public static void DOM(String data) {
        try {
            byte[] b = data.getBytes();
            InputStream inp = new ByteArrayInputStream(b);
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(inp);
            Map<String, String> xmlNode = new HashMap<>();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
